package edu.winona.csclub.testing.gui.imageExport;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JFileChooser;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import edu.winona.csclub.testing.gui.ExtFileFilter;
import edu.winona.csclub.testing.gui.TestsView;

public class JpegTestEncoder {

	public static void save( TestsView tv ) throws IOException{
		JFileChooser sav = new JFileChooser();
		sav.setDialogType(JFileChooser.SAVE_DIALOG);
		sav.setFileFilter(new ExtFileFilter("jpeg"));
		sav.showSaveDialog(tv.getParent() );
		File f = sav.getSelectedFile();
		
	    BufferedImage image = new BufferedImage(tv.getTable().getWidth() , tv.getTable().getHeight(), BufferedImage.TYPE_BYTE_INDEXED); 
	    Graphics g = image.getGraphics();
	 
	    tv.getTable().paint(g);
	 
	    FileOutputStream fos = new FileOutputStream(f);
	 
	    JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(fos);
	
	    fos.close();    
	}
}
